package com.stylefeng.guns.modular.system.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.base.Stopwatch;
import com.stylefeng.guns.modular.system.model.BkStats;
import com.stylefeng.guns.modular.system.service.*;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.system.model.GpDashboard;
import com.stylefeng.guns.modular.system.model.GpDashboardDetail;
import com.stylefeng.guns.modular.system.warpper.AllRealTimeMonitorWarpper;
import com.stylefeng.guns.modular.system.warpper.DashBoardDetailWarpper;
import com.stylefeng.guns.modular.system.warpper.DashBoardWarpper;
import com.stylefeng.guns.modular.system.warpper.FengKTimeMonitorWarpper;
import com.stylefeng.guns.modular.system.warpper.MyRealTimeMonitorWarpper;
import com.stylefeng.guns.modular.system.warpper.RealTimeMonitorWarpper;
import com.stylefeng.guns.modular.system.warpper.YdayTimeMonitorWarpper;

/**
 * 实时盘口控制器
 *
 * @author fengshuonan
 * @Date 2018-06-27 20:41:04
 */
@Controller
@RequestMapping("/realtime/monitor")
public class RealtimeMonitorController extends BaseController {

    private String PREFIX = "/system/realtime/";

    @Autowired
    private IGpcService iGpcService;
    
    @Autowired
    private IMyGpcService iMyGpcService;
    
    @Autowired
    private IRealtimeService realtimeService;
    
    @Autowired
    private ICurFengkouService curFengkouService;
    
    @Autowired
    private  IGpDashboardService  gpDashboardService;
    
    @Autowired
    private IGpDashboardDetailService gpDashboardDetailService;

    @Autowired
    private IBkStatsService iBkStatsService;
    
    @Autowired
    private IYdayGpcService ydayGpcService;

    /**
     * 跳转到实时盘口首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "realtime_monitor.html";
    }
    
    /**
     * 跳转到实时盘口首页
     */
    @RequestMapping("my")
    public String myindex() {
        return PREFIX + "myrealtime_monitor.html";
    }
    
    /**
     * 跳转到实时盘口首页
     */
    @RequestMapping("relation")
    public String relationindex() {
        return PREFIX + "relationrealtime_monitor.html";
    }
    
    /**
     * 跳转到实时盘口首页
     */
    @RequestMapping("fengk")
    public String fengkindex() {
        return PREFIX + "fengkrealtime_monitor.html";
    }

    /**
     * 获取实时监控数据
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String gScode, @RequestParam(required = false) String gTime,@RequestParam(required = false) int type
    		,@RequestParam(required = false) Integer zlje,@RequestParam(required = false) Integer mp,@RequestParam(required = false) Integer wt) {
    	if(wt == null){
    		wt = 30;
		}
    	Map<String,Object> map = new HashMap<String,Object>();
    	List<Map<String, Object>> gdhs = null;
    	if(type == 1){
    		gdhs = iGpcService.selectByCurrentDayGpc(Arrays.asList(StringUtils.split(gScode,",")),gTime,gTime,zlje,mp);
    	}else{
    		gdhs = iGpcService.selectByCurrentDayGpc(Arrays.asList(StringUtils.split(gScode,",")),gTime,DateUtil.getDay(),zlje,mp);
    	}
    	GpDashboard gpDashboard = new GpDashboard();
    	if(type == 1){
    		gpDashboard.setgDate(DateUtil.parseDate(gTime));
    	}else{
    		gpDashboard.setgDate(new Date());
    	}
    	List<Map<String, Object>> dashboards = gpDashboardService.selectListByDate(gpDashboard);
    	GpDashboardDetail dashboardDetail = new GpDashboardDetail();
    	if(type == 1){
    		dashboardDetail.setgTime(DateUtil.parseDate(gTime));
    	}else{
    		dashboardDetail.setgTime(new Date());
    	}
		BkStats bkStats = new BkStats();
		if(type == 1){
			bkStats.setgBkDate(DateUtil.parseDate(gTime));
		}else{
			bkStats.setgBkDate(new Date());
		}
		List<Map<String, Object>> bkStatsList = iBkStatsService.selectListByDate(bkStats);
    	List<Map<String, Object>> dashboardds = gpDashboardDetailService.selectListByDate(dashboardDetail);
    	Map<String,Boolean> keys = getFkMaps(dashboardds);
    	map.put("dashboard", new DashBoardWarpper(dashboards).warp());
    	map.put("dashboardd", new DashBoardDetailWarpper(dashboardds).warp());
    	map.put("gdhs", new RealTimeMonitorWarpper(gdhs,type,keys,wt).warp());
		map.put("bkqd", bkStatsList);
		return map;
    }
    
    private Map<String,Boolean> getFkMaps(List<Map<String, Object>> dashboardMaps){
    	Map<String,Boolean> keys = new HashMap<String, Boolean>();
    	for(int i=0;i<dashboardMaps.size();i++){
    		String key = (String)dashboardMaps.get(i).get("gPlate");
    		boolean isxia = StringUtils.contains(key, "下挫") || StringUtils.contains(key, "下跌") || StringUtils.contains(key, "走弱");
    		String k = StringUtils.substringBefore(key, "板块");
    		if(StringUtils.isEmpty(k)){
    			k = StringUtils.substringBefore(key, "走");
    		}
    		keys.put(k, isxia);
    	}  
    	return keys;
    }
    
    /**
     * 获取实时盘口列表
     */
    @RequestMapping(value = "/alllist")
    @ResponseBody
    public Object alllist() {
    	List<Map<String, Object>> gdhs = realtimeService.selectRealTime();
        return new AllRealTimeMonitorWarpper(gdhs).warp();
    }
    
    /**
     * 获取实时监控数据
     */
    @RequestMapping(value = "/my/list")
    @ResponseBody
    public Object mylist(@RequestParam(required = false) String gScode, @RequestParam(required = false) String gTime,@RequestParam(required = false) int type) {
    	Map<String,Object> map = new HashMap<String,Object>();
    	List<Map<String, Object>> gdhs = iMyGpcService.selectByCurrentDayGpc(Arrays.asList(StringUtils.split(gScode,",")),gTime);
    	GpDashboard gpDashboard = new GpDashboard();
    	gpDashboard.setgDate(new Date());
    	List<Map<String, Object>> dashboards = gpDashboardService.selectListByDate(gpDashboard);
    	GpDashboardDetail dashboardDetail = new GpDashboardDetail();
    	dashboardDetail.setgTime(new Date());
    	List<Map<String, Object>> dashboardds = gpDashboardDetailService.selectListByDate(dashboardDetail);
    	Map<String,Boolean> keys = getFkMaps(dashboardds);
    	map.put("dashboardd", new DashBoardDetailWarpper(dashboardds).warp());
    	map.put("dashboard", new DashBoardWarpper(dashboards).warp());
    	map.put("gdhs", new MyRealTimeMonitorWarpper(gdhs,type,keys).warp());
    	return map;
    }
    
    /**
     * 获取实时监控数据
     */
    @RequestMapping(value = "/fengk/list")
    @ResponseBody
    public Object fengklist(@RequestParam(required = false) String gTime,@RequestParam(required = false) Integer zlje,@RequestParam(required = false) Integer mp) {
    	Map<String,Object> map = new HashMap<String,Object>();
    	List<Map<String, Object>> gdhs = iMyGpcService.selectByCurrentFengKDayGpc(gTime,zlje,mp);
    	GpDashboard gpDashboard = new GpDashboard();
    	gpDashboard.setgDate(DateUtil.parseDate(gTime));
    	List<Map<String, Object>> dashboards = gpDashboardService.selectListByDate(gpDashboard);
    	GpDashboardDetail dashboardDetail = new GpDashboardDetail();
    	dashboardDetail.setgTime(DateUtil.parseDate(gTime));
    	List<Map<String, Object>> dashboardds = gpDashboardDetailService.selectListByDate(dashboardDetail);
    	Map<String,Boolean> keys = getFkMaps(dashboardds);
    	map.put("dashboardd", new DashBoardDetailWarpper(dashboardds).warp());
    	map.put("dashboard", new DashBoardWarpper(dashboards).warp());
    	map.put("fengk", new FengKTimeMonitorWarpper(gdhs,keys).warp());
        return map;
    }
    
    /**
     * 获取昨日涨停
     */
    @RequestMapping(value = "/yday/list")
    @ResponseBody
    public Object ydaylist(@RequestParam(required = false) String gTime,@RequestParam(required = false) Integer zlje,@RequestParam(required = false) Integer mp,@RequestParam(required = false) Integer lbcount
	,@RequestParam(required = false) Integer wt) {
    	if(wt == null){
    		wt = 30;
		}
    	Map<String,Object> map = new HashMap<String,Object>();
    	List<Map<String, Object>> gdhs = ydayGpcService.selectByCurrentDayGpc(gTime,zlje,mp,lbcount);
    	GpDashboard gpDashboard = new GpDashboard();
    	gpDashboard.setgDate(DateUtil.parseDate(gTime));
    	List<Map<String, Object>> dashboards = gpDashboardService.selectListByDate(gpDashboard);
    	GpDashboardDetail dashboardDetail = new GpDashboardDetail();
    	dashboardDetail.setgTime(DateUtil.parseDate(gTime));
    	List<Map<String, Object>> dashboardds = gpDashboardDetailService.selectListByDate(dashboardDetail);
    	Map<String,Boolean> keys = getFkMaps(dashboardds);
		BkStats bkStats = new BkStats();
		bkStats.setgBkDate(DateUtil.parseDate(gTime));
		List<Map<String, Object>> bkStatsList = iBkStatsService.selectListByDate(bkStats);
    	map.put("dashboardd", new DashBoardDetailWarpper(dashboardds).warp());
    	map.put("dashboard", new DashBoardWarpper(dashboards).warp());
    	map.put("fengk", new YdayTimeMonitorWarpper(gdhs,keys,wt).warp());
		map.put("bkqd", bkStatsList);
        return map;
    }
    
    /**
     * 获取实时监控数据
     */
    @RequestMapping(value = "/my/alllist")
    @ResponseBody
    public Object myalllist() {
    	List<Map<String, Object>> gdhs = realtimeService.selectMyRealTime();
        return new AllRealTimeMonitorWarpper(gdhs).warp();
    }
}
